package com.object.equals;

public class EqualsDemo {
    public static void main(String[] args) {
        Student stu1=new Student("张三",25);
        Student stu2=new Student("张三",25);
        System.out.println(stu1.equals(stu2));
    }

}

class Student{
    String name;
    int age;

   public boolean equals(Object obj){
       if(obj instanceof Student){
           // 向下转型，为了调用子类特有的成员
           Student stu=(Student) obj;
           return this.age== stu.age && this.name.equals(stu.name);
       }else {
           return false;
       }
   }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
}
